package com.jingdianjichi.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * 数据库密码加密工具类
 * @author 86156
 * @version 1.0
 * @date 2024-10-02 10:15
 */

public class DruidEncryptUtil {

    private static String publicKey;
    private static String privateKey;

    static{
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            publicKey = keyPair[1];
            System.out.println("publicKey = "+publicKey);
            privateKey = keyPair[0];
            System.out.println("privateKey = "+privateKey);
        } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }

    public static String Encrypt(String plainText) throws Exception {
        String encrypt = ConfigTools.encrypt(privateKey,plainText);
        System.out.println("encrypt:"+encrypt);
        return encrypt;
    }

    public static String decrypt(String cipherText) throws Exception {
        String decrypt = ConfigTools.decrypt(publicKey,cipherText);
        System.out.println("decrypt"+decrypt);
        return decrypt;
    }

    public static void main(String[] args) throws Exception {
        Encrypt("123456");
    }

}
